//===============================================================================
// Microsoft Architecture Strategy Team
// LitwareHR - SaaS Sample Application
//===============================================================================
// Copyright  Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;

namespace DCControls
{
    public partial class DependencyViewControl : UserControl
    {
        private DependencyTest.DependencyBase dependency;
        private Color titleSavedColor;
        private bool dependencyStatus;

        public bool DependencyStatus
        {
            get { return dependencyStatus; }
            set { dependencyStatus = value; }
        }
        public DependencyTest.DependencyBase Dependency
        {
            get { return dependency; }
            set { 
                
                dependency = value;
                lblTitle.Text = dependency.Title;
                lblExplination.Text = dependency.Explination;
                if (!string.IsNullOrEmpty(dependency.DownloadUrl))
                {
                    llDownloadUrl.Links[0].Description = dependency.DownloadUrl;
                    llDownloadUrl.Text = "Download";
                }
                else if(!string.IsNullOrEmpty(dependency.ScriptName))
                {
                    llDownloadUrl.Links[0].Description = dependency.ScriptName;
                    llDownloadUrl.Text = "Install now";
                }

                if (dependency.Check())
                {
                    pictureBox1.Image = global::DCControls.Properties.Resources.Checked;
                    llDownloadUrl.Visible = false;
                    lblExplination.Visible = false;
                    Height = 25;
                    lblTitle.ForeColor = titleSavedColor;
                    dependencyStatus = true;
                }
                else
                {
                    pictureBox1.Image = global::DCControls.Properties.Resources.Unchecked;
                    lblTitle.ForeColor = Color.Red;
                    dependencyStatus = false;

                }

            }
        }
        public DependencyViewControl()
        {
            InitializeComponent();
            llDownloadUrl.Text = "";
            titleSavedColor = lblTitle.ForeColor;

        }

        private void llDownloadUrl_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (!string.IsNullOrEmpty(dependency.DownloadUrl))
            {

                System.Diagnostics.Process.Start(dependency.DownloadUrl);
            }
            else 
            {
                Shell32.ShellClass shell = new Shell32.ShellClass();
                string startPath = System.IO.Path.GetDirectoryName(Application.ExecutablePath);
                string pathToNextScript = System.IO.Path.Combine(startPath, dependency.ScriptName);

                shell.ShellExecute(pathToNextScript, "", System.IO.Path.GetDirectoryName(pathToNextScript), "runas", 9);
            }
        }
    }
}
